package com.qk.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qk.common.PageResult;
import com.qk.dto.BusinessDto;
import com.qk.entity.Business;
import com.qk.entity.BusinessTrackRecord;
import com.qk.entity.Customer;
import com.qk.mapper.BusinessMapper;
import com.qk.mapper.BusinessTrackRecordMapper;
import com.qk.mapper.CustomerMapper;
import com.qk.service.BusinessService;
import com.qk.utils.CurrentUserHoler;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;


@Service
public class BusiniessServiceImpl extends ServiceImpl<BusinessMapper, Business> implements BusinessService {
    @Autowired
    private BusinessMapper businessMapper;
    @Autowired
    private BusinessTrackRecordMapper businessTrackRecordMapper;
    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public PageResult<Business> listBusinesses(BusinessDto businessDto) {
        Page<Business> businessPage = businessMapper.listBusinesses(new Page<Business>(businessDto.getPage(),businessDto.getPageSize()),businessDto);
        return new PageResult<>(businessPage.getTotal(),businessPage.getRecords());
    }

    @Override
    public Business getBusinessById(Integer id) {
        return businessMapper.selectBusinessById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class) // 事务控制 - 默认只有运行期异常和错误才会回滚
    public void trackBusiness(Business business) {
        // 1.根据ID更新线索基本信息
        business.setStatus(3);
        business.setUpdateTime(LocalDateTime.now());
        businessMapper.updateById(business);

        // 2.添加跟进记录
        BusinessTrackRecord businessTrackRecord = new BusinessTrackRecord();
        String keyItems = String.join(",",business.getKeyItems()); // 将数组转换成字符串
        businessTrackRecord.setBusinessId(business.getId());
        businessTrackRecord.setUserId(CurrentUserHoler.getCurrentUser()); // 当前登录用户ID
        businessTrackRecord.setCreateTime(LocalDateTime.now());
        businessTrackRecord.setRecord(business.getRecord());
        businessTrackRecord.setNextTime(business.getNextTime());
        businessTrackRecord.setKeyItems(keyItems);
        businessTrackRecord.setTrackStatus(1);
        businessTrackRecordMapper.insert(businessTrackRecord);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void back(Integer id) {
        // 1.根据ID更新商机基本信息
        Business business = new Business();
        business.setId(id);
        business.setStatus(4);
        business.setUpdateTime(LocalDateTime.now());
        businessMapper.updateById(business);
        // 2.添加商机跟踪记录
        BusinessTrackRecord businessTrackRecord = new BusinessTrackRecord();
        businessTrackRecord.setId(null);
        businessTrackRecord.setBusinessId(id);
        businessTrackRecord.setUserId(CurrentUserHoler.getCurrentUser());
        businessTrackRecord.setTrackStatus(0);
        businessTrackRecord.setRecord("踢回公海");
        businessTrackRecord.setCreateTime(LocalDateTime.now());
        businessTrackRecordMapper.insert(businessTrackRecord);
    }

    @Override
    public void toCustomer(Integer id) {
        // 1.根据ID更新线索状态
        Business business = businessMapper.selectById(id);
        business.setStatus(5);
        business.setUpdateTime(LocalDateTime.now());
        businessMapper.updateById(business);

        // 2.保存客户信息
        Customer customer = new Customer();
        BeanUtils.copyProperties(business, customer);
        customer.setId(null);
        customer.setBusinessId(id);
        customer.setCreateTime(LocalDateTime.now());
        customer.setUpdateTime(LocalDateTime.now());
        customerMapper.insert(customer);
    }

    @Override
    public PageResult<Business> listBusinessesPool(BusinessDto businessDto) {
        Page<Business> businessPage = businessMapper.listBusinessesPool(new Page<Business>(businessDto.getPage(),businessDto.getPageSize()),businessDto);
        return new PageResult<>(businessPage.getTotal(),businessPage.getRecords());
    }
}
